package action;

import com.opensymphony.xwork2.ActionContext;
import lombok.Data;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import pojo.Patient;
import pojo.Registry;
import service.PatientService;
import service.RegistryService;
import util.GsonUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Data
public class PatientAction {
    @Autowired
    private PatientService service;
    private RegistryService registryService;
    private Patient patient;
    private String sex;
    private Integer id;
    public String patAdd(){
        if(sex.equals("男")){
            patient.setSex(1);
        }else{
            patient.setSex(2);
        }
        service.PatientAdd(patient);
        return "success";
    }
    public void queryByCard() throws Exception {
        List<Patient> list= service.queryByCard(patient.getIdCard());
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print(GsonUtil.toJson(list));
    }
    public String queryPat(){
        ActionContext.getContext().getSession().put("ids", id);
        System.out.println(id);
        Patient patient = service.queryPat(id);
        Registry rs=new Registry();
        rs.setId(id);
        registryService.update(rs, 2);
        ActionContext.getContext().getSession().put("pt", patient);
        return "successs";
    }
}
